package com.xuzao.developer.controller;

import com.github.pagehelper.PageInfo;
import com.xuzao.developer.annotation.MethodAnnotation;
import com.xuzao.developer.annotation.ModuleAnnotation;
import com.xuzao.developer.base.model.GridFieldSys;
import com.xuzao.developer.dto.GridFieldSysDTO;
import com.xuzao.developer.service.GridFieldSysService;
import com.xuzao.developer.tools.entity.Pager;
import com.xuzao.developer.utils.BJUIMap;
import com.xuzao.developer.utils.IntegerUtil;
import com.xuzao.developer.vo.BaseResultVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Field;
import java.util.Map;

/**
 * @author xuzao
 * @description
 * @date 2019-09-03 17:12:57
 **/
@ModuleAnnotation(name = "表头字段")
@Controller
@RequestMapping("/gridFieldSys/")
public class GridFieldSysController extends BaseController {

    @Resource
    private GridFieldSysService gridFieldSysService;

    @RequestMapping("queryList")
    public String queryList(GridFieldSysDTO dto, Pager pager, Model model) {
        super.initPager(pager);

        PageInfo<GridFieldSys> list = gridFieldSysService.list(dto, pager);

        model.addAttribute("pageInfo", list);
        model.addAttribute("bo", dto);

        return "/grid_field_sys/grid_field_list";
    }


    @MethodAnnotation(name = "单个保存")
    @RequestMapping("saveSingle")
    @ResponseBody
    public BJUIMap saveSingle(HttpServletRequest request) {
        BJUIMap bjuiMap = new BJUIMap();
        bjuiMap.setStatusCode(BJUIMap.CODE_OK);
        bjuiMap.setMessage("操作成功");
        bjuiMap.setTabid("gridFieldSys");

        Map<String, String[]> map = request.getParameterMap();

        GridFieldSysDTO gridFieldSysDTO = new GridFieldSysDTO();

        for (String key : map.keySet()) {
            String val = map.get(key)[0];
            String strFiled = key.split("\\.")[1];

            Field[] fields = gridFieldSysDTO.getClass().getDeclaredFields();
            for (Field field : fields) {
                if (IntegerUtil.equalTo(field.getName(), strFiled) && StringUtils.isNotBlank(val)) {
                    try {
                        field.setAccessible(true);
                        if (Integer.class == field.getType()) {
                            field.set(gridFieldSysDTO, Integer.valueOf(val));
                        } else if (Long.class == field.getType()) {
                            field.set(gridFieldSysDTO, Long.valueOf(val));
                        } else {
                            field.set(gridFieldSysDTO, val);
                        }

                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

        if (gridFieldSysDTO.getId() == null) {
            gridFieldSysService.insert(gridFieldSysDTO);
        } else {
            gridFieldSysService.update(gridFieldSysDTO);
        }
        return bjuiMap;
    }

    @MethodAnnotation(name = "批量保存")
    @RequestMapping("save")
    @ResponseBody
    public BJUIMap save(HttpServletRequest request) {
        BJUIMap bjuiMap = new BJUIMap();
        bjuiMap.setStatusCode(BJUIMap.CODE_OK);
        bjuiMap.setMessage("操作成功");
        bjuiMap.setTabid("gridFieldSys");
        Map<String, String[]> map = request.getParameterMap();
        GridFieldSysDTO[] gridFieldSysDTOS = new GridFieldSysDTO[map.size() / 5];
        Long gridId = null;

        if (map.size() > 0) {
            for (String key : map.keySet()) {
                Integer index = null;
                String strFiled = null;
                String val = null;

                val = map.get(key)[0];

                if (IntegerUtil.equalTo("gridId", key)) {
                    gridId = Long.valueOf(val);
                    continue;
                }

                index = Integer.valueOf(key.split("\\.")[0]);
                strFiled = key.split("\\.")[1];


                GridFieldSysDTO setBean = gridFieldSysDTOS[index];

                if (setBean == null) {
                    setBean = new GridFieldSysDTO();
                }
                Field[] fields = setBean.getClass().getDeclaredFields();
                for (Field field : fields) {
                    if (IntegerUtil.equalTo(field.getName(), strFiled) && StringUtils.isNotBlank(val)) {
                        try {
                            field.setAccessible(true);
                            if (Integer.class == field.getType()) {
                                field.set(setBean, Integer.valueOf(val));
                            } else if (Long.class == field.getType()) {
                                field.set(setBean, Long.valueOf(val));
                            } else {
                                field.set(setBean, val);
                            }

                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        }
                    }
                }
                gridFieldSysDTOS[index] = setBean;
            }
        }

        for (GridFieldSysDTO gridFieldSysDTO : gridFieldSysDTOS) {
            gridFieldSysDTO.setGridId(gridId);
            if (gridFieldSysDTO.getId() == null) {
                gridFieldSysService.insert(gridFieldSysDTO);
            } else {
                gridFieldSysService.update(gridFieldSysDTO);
            }
        }

        return bjuiMap;
    }

    @MethodAnnotation(name = "删除")
    @RequestMapping("del")
    @ResponseBody
    public BJUIMap del(Long id) {
        BJUIMap bjuiMap = new BJUIMap();
        bjuiMap.setStatusCode(BJUIMap.CODE_OK);
        bjuiMap.setMessage("操作成功");
        bjuiMap.setTabid("gridFieldSys");
        gridFieldSysService.del(id);

        return bjuiMap;
    }

    @RequestMapping("addGridFieldSys")
    public BaseResultVo addGridFieldSys(GridFieldSysDTO dto) {
        return gridFieldSysService.insert(dto);
    }

    @RequestMapping("updateGridFieldSys")
    public BaseResultVo updateGridFieldSys(GridFieldSysDTO dto) {
        return gridFieldSysService.update(dto);
    }

    @RequestMapping("getGridFieldSys")
    public BaseResultVo getGridFieldSys(GridFieldSysDTO dto) {
        return gridFieldSysService.get(dto);
    }

}